Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/vba/15.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
VBA:向列添加字符并调整范围大小_Vba_Excel - Fatal编程技术网

VBA:向列添加字符并调整范围大小

VBA:向列添加字符并调整范围大小,vba,excel,Vba,Excel,我正在编写一个宏,希望能将单元格A1、A2、A3、A4、A5、A6…中的值分别复制到列C1、D1、E1、C2、D2、E2…中。 我还想在每个复制的单元格前后添加字符。 这是我目前在阅读和其他一些网站后得到的 Sub reformat() Dim c As Range dim destination As Range Set destination=Range("C1") Set destination=destination.Resize(3,33) For Each c In Selectio

我正在编写一个宏,希望能将单元格A1、A2、A3、A4、A5、A6…中的值分别复制到列C1、D1、E1、C2、D2、E2…中。 我还想在每个复制的单元格前后添加字符。 这是我目前在阅读和其他一些网站后得到的

Sub reformat()
Dim c As Range
dim destination As Range
Set destination=Range("C1")
Set destination=destination.Resize(3,33)
For Each c In Selection
If c.Value<>" " Then destination.Value="beginning"&c.Value&"ending"
Next
End Sub
现在,旧问题不再出现,但只填充了单元格C1。

In

Set destination=Range("C1") 
Set destination=destination.Resize(3,33)
您应该只使用
set
一次来初始化
目标
对象

我想你应该说

destination.resize(3,33)
编辑

尺寸c作为范围
将目的地变暗为范围
设置目的地=范围(“C1”)
目的地。调整大小(3,33)
I=0
对于选择中的每个c
如果c.值为“”,则
destination.offset(i).Value=“开始”和c.Value&“结束”
恩迪夫
I=I+1
下一个
端接头

此代码将所选值复制到[M1]中左上角的3列矩阵中:

Sub reformat()
' copy cell values from selection into 3-column wide destination
' http://stackoverflow.com/questions/34473802/vba-add-char-to-a-column-and-resize-the-range#34473802
' 2015-12-26
    Dim c As Range, destination As Range
    Dim ct As Long

    ct = Selection.Cells.Count
    Set destination = Range("M1").Resize(CInt((ct + 1) / 3), 3)
    ct = 0
    For Each c In Selection
        If c.Value <> "" Then
            destination(CInt(ct \ 3) + 1, (ct Mod 3) + 1).Value = "beginning" & c.Value & "ending"
            ct = ct + 1
        End If
    Next
End Sub
Sub-reformat()
'将所选单元格值复制到3列宽的目标中
' http://stackoverflow.com/questions/34473802/vba-add-char-to-a-column-and-resize-the-range#34473802
' 2015-12-26
尺寸c作为范围,目的地作为范围
像长的一样暗的ct
ct=Selection.Cells.Count
设置目的地=范围(“M1”)。调整大小(CInt((ct+1)/3),3)
ct=0
对于选择中的每个c
如果c.值为“”,则
目的地(CInt(ct\3)+1,(ct Mod 3)+1)。Value=“开始”和c.Value&“结束”
ct=ct+1
如果结束
下一个
端接头
尺寸c作为范围
将目的地变暗为范围
设置目的地=范围(“C1”)
i=0
对于选择中的每个c
如果c.值为“”,则
destination.Offset(i\3,i Mod 3).Value=“开始”和c.Value&“结束”
如果结束
i=i+1
下一个

我无法复制此内容。哪一行抛出了错误?您的目的地没有改变。考虑使用<代码> Test.Office(RoWixEngult.Value=某物< /代码>,然后递增<代码> RooChans此错误是针对<代码>如果C.值“我已更改,但如果c.Value“”,则第
行仍有相同的错误消息。
在运行宏之前是否选择了A1到A6?如果总是这个范围,您可以用
范围(“A1:A6”)
替换
选择项
。此外,如果c.value”“是,我选择了,您可能应该删除
中的空格。我修正了一个输入错误,现在前面的错误信息不再出现,但问题是只填充了单元格C1。请参阅我对您的问题的评论,关于
。offset
Dim c As Range
dim destination As Range
Set destination=Range("C1")
destination.Resize(3,33)
I=0
For Each c In Selection
   If c.Value<>"" Then    
      destination.offset(i).Value="beginning"&c.Value&"ending"
   Endif
   I=i+1
Next
End Sub
Sub reformat()
' copy cell values from selection into 3-column wide destination
' http://stackoverflow.com/questions/34473802/vba-add-char-to-a-column-and-resize-the-range#34473802
' 2015-12-26
    Dim c As Range, destination As Range
    Dim ct As Long

    ct = Selection.Cells.Count
    Set destination = Range("M1").Resize(CInt((ct + 1) / 3), 3)
    ct = 0
    For Each c In Selection
        If c.Value <> "" Then
            destination(CInt(ct \ 3) + 1, (ct Mod 3) + 1).Value = "beginning" & c.Value & "ending"
            ct = ct + 1
        End If
    Next
End Sub
    Dim c As Range
    Dim destination As Range
    Set destination = Range("C1")

    i = 0
    For Each c In Selection
       If c.Value <> " " Then
          destination.Offset(i \ 3, i Mod 3).Value = "beginning" & c.Value & "ending"
       End If
       i = i + 1

Next