Combine Data in Multiple Columns Into One Column

usmananyc

New Member
Joined
Jul 21, 2015
Messages
31
Hello Everyone, thank you in advance for your help.

Basically what i am trying to do is copy all data from Columns M to Column T starting from Row 3 and paste it into a new worksheet. The hard part is that some cell values can be blank in between rows. So essentially id like to copy all values without any blanks into the new worksheet. My current script looks at the whole range instead of starting at M3.

If its not too much trouble, id also like to output the newly pasted data to a .SQL file and save it in a certain directory.

Code:
Sub CopyData()

' CopyData Macro


Dim x As Integer
Dim y As Integer


x = 1
y = 1


Do Until Worksheets("Datafix").Range("M" & x) = ""
    Worksheets("SQLScript").Range("A" & y) = Worksheets("Datafix").Range("M" & x)
    y = y + 1
    x = x + 1
    
Loop


End Sub

Example:
[TABLE="width: 150"]
<tbody>[TR]
[TD]Update[/TD]
[TD]Start[/TD]
[TD]End[/TD]
[TD]Remove[/TD]
[TD]Volume[/TD]
[TD]Cost[/TD]
[TD]Append[/TD]
[TD]Void[/TD]
[/TR]
[TR]
[TD]A[/TD]
[TD]D[/TD]
[TD]G[/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD]D[/TD]
[TD][/TD]
[/TR]
[TR]
[TD][/TD]
[TD]D[/TD]
[TD]G[/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[/TR]
[TR]
[TD]B[/TD]
[TD]D[/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[/TR]
[TR]
[TD]B[/TD]
[TD][/TD]
[TD]G[/TD]
[TD][/TD]
[TD]C[/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[/TR]
[TR]
[TD]C[/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[/TR]
[TR]
[TD]C[/TD]
[TD][/TD]
[TD]G[/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[/TR]
[TR]
[TD][/TD]
[TD]D[/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD]D[/TD]
[TD][/TD]
[/TR]
</tbody>[/TABLE]
 

Excel Facts

Best way to learn Power Query?
Read M is for (Data) Monkey book by Ken Puls and Miguel Escobar. It is the complete guide to Power Query.
Do those columns contain any formulae?
Also do you want all of them to go to col A?
 
Upvote 0
Do those columns contain any formulae?
Also do you want all of them to go to col A?

These columns output a SQL Script from running a macro so it gets output as text i believe, no formulas in the cell itself. And Yes, id like all of them to go to Col A.
 
Upvote 0
Try this
Code:
Sub CopyColumns()

   Dim Cnt As Long
   
   For Cnt = 13 To 20
      Worksheets("Datafix").Columns(Cnt).SpecialCells(xlConstants).Copy _
         Worksheets("SQLScript").Range("A" & Rows.Count).End(xlUp).Offset(1)
   Next Cnt
End Sub
Unfortunately I cannot help with the 2nd part of your request.
 
Upvote 0
Try this
Code:
Sub CopyColumns()

   Dim Cnt As Long
   
   For Cnt = 13 To 20
      Worksheets("Datafix").Columns(Cnt).SpecialCells(xlConstants).Copy _
         Worksheets("SQLScript").Range("A" & Rows.Count).End(xlUp).Offset(1)
   Next Cnt
End Sub
Unfortunately I cannot help with the 2nd part of your request.

Wow that works great! Didnt expect it to be such a simple code. Thank you so much! Now i only need this outputed as a .SQL or .TXT file.
 
Upvote 0

Forum statistics

Threads
1,224,823
Messages
6,181,181
Members
453,021
Latest member
Justyna P

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top