Excel 修剪不同图纸VBA中行和列中的单元格值
我想修剪有空间的单元格。但我现在的宏不起作用Excel 修剪不同图纸VBA中行和列中的单元格值,excel,vba,trim,spaces,Excel,Vba,Trim,Spaces,我想修剪有空间的单元格。但我现在的宏不起作用 Sub DoSomething() 'Declare Variables Dim i, r, c, count, lastColData, LastRowData, lastRowInput, StartSearch As Long Dim shtData, shtInput, shtInputI, shtInputII, shtInputIII, shtInputIV, shtInputV, shtInputVI, sht
Sub DoSomething()
'Declare Variables
Dim i, r, c, count, lastColData, LastRowData, lastRowInput, StartSearch As Long
Dim shtData, shtInput, shtInputI, shtInputII, shtInputIII, shtInputIV, shtInputV, shtInputVI, shtCopy As Worksheet
Dim Cell As Range
Set shtData = Sheets("PQFR")
Set shtInput = Sheets("INPUT")
Set shtInputI = Sheets("INPUTI")
Set shtInputII = Sheets("INPUTII")
Set shtInputIII = Sheets("INPUTIII")
Set shtInputIV = Sheets("INPUTIV")
Set shtInputV = Sheets("INPUTV")
Set shtInputVI = Sheets("INPUTVI")
Set shtCopy = Sheets("INPUTR")
lastColData = shtData.Cells(4, shtData.Columns.count).End(xlToLeft).Column
LastRowData = shtData.Cells(shtData.Rows.count, "A").End(xlUp).row
lastRowInput = shtInputI.Cells(shtInputI.Rows.count, "A").End(xlUp).row
'lastRowData = 10
'Remove spaces from these sheets:
shtInput.Cells(r, 32).Value = Trim(shtInput.Cell(r, 32).Value)
shtInputI.Cells(4, c).Value = Trim(shtInputI.Cells(4, c)).Value
shtInputII.Cells(4, c).Value = Trim(shtInputII.Cell(4, c)).Value
shtInputIII.Cells(4, c).Value = Trim(shtInputIII.Cell(4, c)).Value
shtInputIV.Cells(4, c).Value = Trim(shtInputIV.Cells(4, c)).Value
End Sub
那是因为你没有做循环。如果你能更好地向我解释你想要什么,我可以让你想怎么做就怎么做
Dim i, r, c, count, lastColData, LastRowData, lastRowInput, StartSearch As Long
For Each Worksheet In ActiveWorkbook.Worksheets
If Worksheet.Name = "PQFR" Or Worksheet.Name = "INPUT" Then 'please do for the sheets you want your command gonna`s be executed
Sheets(Worksheet.Name).Select
lastColData = Cells(4,Columns.count).End(xlToLeft).Column
LastRowData = Cells(Rows.count, 1).End(xlUp).Row
lastRowInput = Cells(Rows.count, 1).End(xlUp).Row
'using c=32 just for input
If Worksheet.Name = "INPUT" Then
'using r = 4 fosheets
For c = 32 To lastColData
For r = 1 To LastRowData
Cells(r, c) = Trim(Cells(r, c).Value)
Next r
Next c
Else
'using r = 4 for all the other sheets
For c = 1 To lastColData
For r = 4 To LastRowData
Cells(r, c) = Trim(Cells(r, c).Value)
Next r
Next c
End If
End If
Next Worksheet
请注意,“不工作”不是有效的错误描述。告诉你得到的错误和在哪里。还请注意,
Dim i,r,c,count,lastColData,LastRowData,lastRowInput,StartSearch As Long
仅将最后一个变量声明为Long
,而将所有其他变量声明为Variant
您需要为每个变量指定一种类型:Dim i As Long,r As Long,c As Long,count As Long,lastColData As Long,lastRowInput As Long,StartSearch As Long
还要注意Trim(shtInputI.Cells(4,c)).Value
无法工作,因为Trim
没有。Value
括号设置在错误的位置。在执行过程中,我得到的错误是1004错误,由于设备或错误定义的对象。这在第一个修剪功能上显示为黄色标记。当然,因为您没有为r
定义值,所以它是r=0
,并且行0
不存在。此外,您还应该修复我上面提到的问题和问题中的代码。c也没有值,原因与@P相同ᴇʜ提到Dim i,r,c,count,lastColData,LastRowData,lastRowInput,StartSearch As Long
仅将最后一个变量声明为Long
,而将所有其他变量声明为Variant
您需要为每个变量指定一种类型:Dim i As Long,r As Long,c As Long,count As Long,lastColData As Long,LastRowData尽可能长,lastRowInput尽可能长,StartSearch尽可能长