Macro to insert double quotes

Jimf2

New Member
Joined
Jan 8, 2018
Messages
7
Hi, I need to export a sheet to CSV format, so I can use that csv file as input to a access table SQL insert statement.
My problem is, I have one column that has data in it with quotes that cannot be altered.
e.g. The "location" field has building, room, cubicle such as "Building 208, Room 19, Cubicle 9"
This causes the building, room and cubicle to be placed in a separate cell. I need them as a single cell in the output. Placing double quotes in that cell before export solves the problem.
I have a macro that will insert the double quotes but there are some rows that are blank in the column.


This is what I've tried:
Line 2 when enabled selects all rows until it reaches a blank cell
Line 3 when enabled selects all rows in the sheet, well below the last line of real data

Code:
Sub AddQuotes()
'With Range(Range("G1"), Range("G1").End(xlDown)).Select
With Range("G:G").Select
On Error Resume Next
Dim myCell As Range
     For Each myCell In Selection
             myCell.Value = Chr(34) & myCell.Value & Chr(34)
     Next myCell


End With
End Sub
Here's the problem, using the code as shown works until the column G has a blank cell (at row 10), then the writing of the double quotes terminates before the bottom of the sheet (200 rows).

When I use the 2nd line instead of the third to define the range, I get the whole column double quoted well past the 200 rows of data.

How can I just get the double quotes on the "count of rows" in column G.
Note: if it helps, I can reference another row that has no empty cells for the 200 row count.
Also note the row count figure 200 is really variable, one week it may be 200, the next may be 375, the next may be 225.
 

Excel Facts

Get help while writing formula
Click the italics "fx" icon to the left of the formula bar to open the Functions Arguments dialog. Help is displayed for each argument.
What if you created a macro that applies to everything in a selected range? It would apply to everything in ctrl+shift+end, i.e. the range you are focusing on, regardless of column, blanks, etc....

Code:
Sub AddQuotes()

Dim rng As Range

Set rng = Selection
With rng
On Error Resume Next
Dim myCell As Range
     For Each myCell In rng
             myCell.Value = Chr(34) & myCell.Value & Chr(34)
     Next myCell


End With
End Sub
 
Upvote 0
What should happen for blank cells... nothing or a pair of quotes only?

Hi Rick, thanks for the reply.

Some cells will be blank, but the adjacent cells will have data, so those blank cells will need empty double quotes

e.g
FirstNameLastNameExtensionAddress
FredJones4450Bldg 20, Rm6, Cubicle8
SherryPeters2391Bldg 20, Rm6, Cubicle9
DaveWrite4213Bldg 21, Rm1, Cubicle2
DianneGrey6697Bldg 21, Rm2, Cubicle1
BettyWhite4540
GarryBall3578Bldg 22, Rm4, Cubicle1
WilliamStone4179Bldg 22, Rm4, Cubicl2
JoycePatten3527Bldg 22, Rm4, Cubicle5
TerryMcMillan8573Bldg 22, Rm4, Cubicle7
BarryWilliams5567Bldg 23, Rm2, Cubicle3
GraceLee2314Bldg 23, Rm2, Cubicle4

<tbody>
</tbody>


Notice Betty White's location is blank, but the macro should input double quotes in that cell.
To me it seems I need to test for the existence of data in a known column (like phone extension) because it will always have data and if data exists there then double quote the blank cell, if the extension is blank AND location is blank then end the macro.
 
Upvote 0
Hi kbrummert,
Yes, I tried that code you quoted, but it double quotes the rows beyond the bottom of the data. If I only have 200 rows, the double quoted column "G" has double quotes from row 1 to well beyond the last valid row of data. The macro will be execute outside the excel spreadsheet (in powershell) so if there is a way for the macro to do the ctl+shift+end, that is what is needed.
 
Upvote 0
Try this.
Code:
Sub AddQuotes()

     For Each myCell In Range(Range("G1"), Range("G" & Rows.Count).End(xlUp))
             myCell.Value = Chr(34) & myCell.Value & Chr(34)
     Next myCell

End Sub
 
Upvote 0
You can also use this non-looping macro to produce the same results...
Code:
Sub AddQuotes()
  Dim Addr As String
  Addr = Range("G1", Cells(Rows.Count, "G").End(xlUp)).Address(0, 0)
  Range(Addr) = Evaluate("IF({1},""""""""&" & Addr & "&"""""""")")
End Sub
 
Upvote 0

Forum statistics

Threads
1,214,792
Messages
6,121,612
Members
449,039
Latest member
Mbone Mathonsi

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