VBA to add quotation marks around all cell values in a sheet?

XcelNoobster

New Member
Joined
Jun 7, 2022
Messages
40
So I am looking to create a macro that adds quotations marks around all cell values in a sheet. How would I do that?
 

Excel Facts

Can you sort left to right?
To sort left-to-right, use the Sort dialog box. Click Options. Choose "Sort left to right"
The following macro assumes that the worksheet containing the data is the active sheet, and that the data starts at cell A1...

VBA Code:
Sub AddQuotes()

    Dim lastRow As Long
    lastRow = Cells(Rows.Count, "A").End(xlUp).Row
   
    Dim lastCol As Long
    lastCol = Cells(1, Columns.Count).End(xlToLeft).Column
   
    Dim rng As Range
    Set rng = Range("A1", Cells(lastRow, lastCol))
   
    Dim cell As Range
    For Each cell In rng
        cell.Value = """" & cell.Value & """"
    Next cell
   
End Sub

By the way, I would suggest that you update your profile to include the version of Excel you're using, as it may help in future in providing the most efficient solution.

Hope this helps!
 
Upvote 0
If the data in the worksheet goes beyond the row and column limits of A1, then the range defined by Domenic's line of code may not include all the cells with data in the worksheet.

To ensure that all cells with data are included in the range, you can use the UsedRange property of the worksheet, as shown in below code.
I'm not a VBA expert, so try this in a copy of your workbook.

VBA Code:
Sub AddQuotesToUsedRange()
    Dim cell As Range
    Dim myRange As Range
    
    Set myRange = ActiveSheet.UsedRange
    
    For Each cell In myRange
        If Not IsEmpty(cell.Value) Then
            cell.Value = Chr(34) & cell.Value & Chr(34)
        End If
    Next cell
End Sub

Alternatively, you can use the find function to define the last row and column.
 
Upvote 0

Forum statistics

Threads
1,223,236
Messages
6,170,906
Members
452,366
Latest member
TePunaBloke

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