【发布时间】:2012-04-12 17:03:31
【问题描述】:
在一些帮助下,我将两个函数组合在一起,它们可以协同工作,首先将我的所有数据从“文本”格式转换为“数字”格式。之后,它将每列设置为固定数量的字符。
下面列出了我正在使用的两个子例程,但我不知道如何为各个函数省略某些行/列。
运行 psAdd 函数时,我想省略范围的前 3 行,而对于 FormatFixedNumber 函数,我想省略几列。后者的问题是我有 1000+ 列数据和一个包含 1 或 0 的关键标题行,表示是否应转换该列。
如何修改此代码以跳过第一个 sub 中的前 3 行,以及第二个中标记为 0 的几列?
Sub psAdd()
Dim x As Range 'Just a blank cell for variable
Dim z As Range 'Selection to work with
Set z = Cells
Set x = Range("A65536").End(xlUp).Offset(1)
If x <> "" Then
Exit Sub
Else
x.Copy
z.PasteSpecial Paste:=xlPasteAll, Operation:=xlAdd
Application.CutCopyMode = False 'Kill copy mode
End If
x.ClearContents 'Back to normal
End Sub
Sub FormatFixedNumber()
Dim i As Long
Application.ScreenUpdating = False
For i = 1 To lastCol 'replace 10 by the index of the last column of your spreadsheet
With Columns(i)
.NumberFormat = String(.Cells(2, 1), "0") 'number length is in second row
End With
Next i
Application.ScreenUpdating = True
End Sub
【问题讨论】: