复制和粘贴时保留前导零
Retaining Leading Zero when Copy & Pasting
我有以下代码将数据从一个 sheet 复制并粘贴到另一个。
如果您需要更多详细信息:
我遇到的主要问题是在拆分成单独的行后保留尺寸的前导零。
COL_SIZE in "SS21 Master Sheet"= 06|612|1218|1824
Column "AH" in "Buysheet" is = 6
612
1218
1824
我如何将 AH 列设置为文本以使其保留零 (06)?我已经尝试了几个选项,但其中 none 目前正在工作。
Private Sub Workbook_Open()
Sheets("BUYSHEET").Cells.Clear
Const COL_SIZE As String = "AO" 'This is the column with all the sizes listed in the mini master
Dim wb1 As Workbook, wsSource As Worksheet
Set wb1 = Workbooks.Open("U:\Design\KIDS\SS21 Kids Miniscale (Master) .xlsm") ' This is the file path to your mini master.
Dim wb2 As Workbook, wsTarget As Worksheet
Set wb2 = ThisWorkbook
Dim iLastRow As Long, iTarget As Long, iRow As Long
Dim rngSource As Range, ar As Variant, i As Integer
Set wsSource = wb1.Sheets("SS21 Master Sheet") ' This is the name of your manster tab
Set wsTarget = wb2.Sheets("BUYSHEET") 'this ths the name of your buysheet tab
iLastRow = wsSource.Range("A" & Rows.Count).End(xlUp).Row
iTarget = wsTarget.Range("A" & Rows.Count).End(xlUp).Row
With wsSource
For iRow = 1 To iLastRow
Set rngSource = Intersect(.Rows(iRow).EntireRow, .Range("A:C, E:Y, Z:AF, AH:AI, AO:AO")) 'This columns you want to pull though to the buysheet tab (if one column must still be range eg, AO:AO)
If iRow = 1 Then
rngSource.Copy wsTarget.Range("A1")
iTarget = iTarget + 1
Else
ar = Split(.Range(COL_SIZE & iRow), "|")
For i = 0 To UBound(ar)
rngSource.Copy wsTarget.Cells(iTarget, 1)
wsTarget.Range("AH" & iTarget).Value = ar(i) 'AI is the column the sizes will populate in - We want this to replace the size list
iTarget = iTarget + 1
Next
End If
Next
MsgBox "Completed"
End With
好的,我明白了。
wsTarget.Range("AH" & iTarget).Value = ar(i)
如果写的是 06
,那么 Excel 会将其视为一个数字并使其成为 6
。
你可以"format as text"这样,在你写值之前:
With wsTarget.Range("AH" & iTarget)
.NumberFormat = "@"
.Value = ar(i)
End With
或者您可以在值前加上撇号/单引号:
wsTarget.Range("AH" & iTarget).Value = "'" & ar(i)
任你选择。
我有以下代码将数据从一个 sheet 复制并粘贴到另一个。
如果您需要更多详细信息:
我遇到的主要问题是在拆分成单独的行后保留尺寸的前导零。
COL_SIZE in "SS21 Master Sheet"= 06|612|1218|1824
Column "AH" in "Buysheet" is = 6
612
1218
1824
我如何将 AH 列设置为文本以使其保留零 (06)?我已经尝试了几个选项,但其中 none 目前正在工作。
Private Sub Workbook_Open()
Sheets("BUYSHEET").Cells.Clear
Const COL_SIZE As String = "AO" 'This is the column with all the sizes listed in the mini master
Dim wb1 As Workbook, wsSource As Worksheet
Set wb1 = Workbooks.Open("U:\Design\KIDS\SS21 Kids Miniscale (Master) .xlsm") ' This is the file path to your mini master.
Dim wb2 As Workbook, wsTarget As Worksheet
Set wb2 = ThisWorkbook
Dim iLastRow As Long, iTarget As Long, iRow As Long
Dim rngSource As Range, ar As Variant, i As Integer
Set wsSource = wb1.Sheets("SS21 Master Sheet") ' This is the name of your manster tab
Set wsTarget = wb2.Sheets("BUYSHEET") 'this ths the name of your buysheet tab
iLastRow = wsSource.Range("A" & Rows.Count).End(xlUp).Row
iTarget = wsTarget.Range("A" & Rows.Count).End(xlUp).Row
With wsSource
For iRow = 1 To iLastRow
Set rngSource = Intersect(.Rows(iRow).EntireRow, .Range("A:C, E:Y, Z:AF, AH:AI, AO:AO")) 'This columns you want to pull though to the buysheet tab (if one column must still be range eg, AO:AO)
If iRow = 1 Then
rngSource.Copy wsTarget.Range("A1")
iTarget = iTarget + 1
Else
ar = Split(.Range(COL_SIZE & iRow), "|")
For i = 0 To UBound(ar)
rngSource.Copy wsTarget.Cells(iTarget, 1)
wsTarget.Range("AH" & iTarget).Value = ar(i) 'AI is the column the sizes will populate in - We want this to replace the size list
iTarget = iTarget + 1
Next
End If
Next
MsgBox "Completed"
End With
好的,我明白了。
wsTarget.Range("AH" & iTarget).Value = ar(i)
如果写的是 06
,那么 Excel 会将其视为一个数字并使其成为 6
。
你可以"format as text"这样,在你写值之前:
With wsTarget.Range("AH" & iTarget)
.NumberFormat = "@"
.Value = ar(i)
End With
或者您可以在值前加上撇号/单引号:
wsTarget.Range("AH" & iTarget).Value = "'" & ar(i)
任你选择。