Macro to add quotes, commas, transpose and select the transposed rows

sncb

Board Regular
Joined
Mar 17, 2011
Messages
145
Office Version
  1. 365
  2. 2010
Platform
  1. Windows
Hi All,

I am constantly on a daily basis having to prepare data for SQL in excel so was wondering if there is an automatic way to add quotes, commas, transpose and select the transposed rows which i could simply paste into SQL.

Here's what I do:
1. I paste all my data in column B1 onwards
2. Add quotes A1 and C1
3. Add commas in D1
4. Concatenate in E1
5. Copy down the formula to all the rows of data
6. Copy column of data in E and paste values in F
7. Select all the values in F and Transpose into rows from cell G2 onwards.

Since I do this very often, is there a better way using a macro such that I can simply paste the column of data in B1 each time and run the macro so that it does the above steps without all the manual work involved?

Thanks for any assistance towards this manual effort.
 
Thanks Dreid. That worked perfectly well.

The output did have double quotes on the left side of each item that was transposed but I adjusted the macro accordingly.

Changed from combo(i) = "''" to combo(i) = "'"

Works great. Thank you for your time and effort and for easing my daily work.
Please ignore this part: Changed from combo(i) = "''" to combo(i) = "'"

I reset it back to your original code.
 
Upvote 0

Excel Facts

How to show all formulas in Excel?
Press Ctrl+` to show all formulas. Press it again to toggle back to numbers. The grave accent is often under the tilde on US keyboards.
If you wanted to consider a non-looping version ..

VBA Code:
Sub Test()
  Dim a As Variant
  a = Split("''" & Join(Application.Transpose(Range("B1", Range("B" & Rows.Count).End(xlUp)).Value), "',|''") & "'", "|")
  Range("G2").Resize(, UBound(a) + 1).Value = a
End Sub
 
Upvote 1
If you wanted to consider a non-looping version ..

VBA Code:
Sub Test()
  Dim a As Variant
  a = Split("''" & Join(Application.Transpose(Range("B1", Range("B" & Rows.Count).End(xlUp)).Value), "',|''") & "'", "|")
  Range("G2").Resize(, UBound(a) + 1).Value = a
End Sub
Hi Peter, Many thanks for your help as well. Your code works perfectly well too. thank you.
 
Upvote 0

Forum statistics

Threads
1,215,069
Messages
6,122,954
Members
449,095
Latest member
nmaske

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