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

Whats the difference between CONCAT and CONCATENATE?
The newer CONCAT function can reference a range of cells. =CONCATENATE(A1,A2,A3,A4,A5) becomes =CONCAT(A1:A5)
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
[TABLE="width: 398"]
<tbody>[TR]
[TD]FirstName[/TD]
[TD]LastName[/TD]
[TD]Extension[/TD]
[TD]Address[/TD]
[/TR]
[TR]
[TD]Fred[/TD]
[TD]Jones[/TD]
[TD="align: right"]4450[/TD]
[TD]Bldg 20, Rm6, Cubicle8[/TD]
[/TR]
[TR]
[TD]Sherry[/TD]
[TD]Peters[/TD]
[TD="align: right"]2391[/TD]
[TD]Bldg 20, Rm6, Cubicle9[/TD]
[/TR]
[TR]
[TD]Dave[/TD]
[TD]Write[/TD]
[TD="align: right"]4213[/TD]
[TD]Bldg 21, Rm1, Cubicle2[/TD]
[/TR]
[TR]
[TD]Dianne[/TD]
[TD]Grey[/TD]
[TD="align: right"]6697[/TD]
[TD]Bldg 21, Rm2, Cubicle1[/TD]
[/TR]
[TR]
[TD]Betty[/TD]
[TD]White[/TD]
[TD="align: right"]4540[/TD]
[TD][/TD]
[/TR]
[TR]
[TD]Garry[/TD]
[TD]Ball[/TD]
[TD="align: right"]3578[/TD]
[TD]Bldg 22, Rm4, Cubicle1[/TD]
[/TR]
[TR]
[TD]William[/TD]
[TD]Stone[/TD]
[TD="align: right"]4179[/TD]
[TD]Bldg 22, Rm4, Cubicl2[/TD]
[/TR]
[TR]
[TD]Joyce[/TD]
[TD]Patten[/TD]
[TD="align: right"]3527[/TD]
[TD]Bldg 22, Rm4, Cubicle5[/TD]
[/TR]
[TR]
[TD]Terry[/TD]
[TD]McMillan[/TD]
[TD="align: right"]8573[/TD]
[TD]Bldg 22, Rm4, Cubicle7[/TD]
[/TR]
[TR]
[TD]Barry[/TD]
[TD]Williams[/TD]
[TD="align: right"]5567[/TD]
[TD]Bldg 23, Rm2, Cubicle3[/TD]
[/TR]
[TR]
[TD]Grace[/TD]
[TD]Lee[/TD]
[TD="align: right"]2314[/TD]
[TD]Bldg 23, Rm2, Cubicle4[/TD]
[/TR]
</tbody>[/TABLE]


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,223,911
Messages
6,175,325
Members
452,635
Latest member
laura12345

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