Trouble with Tables

Philip1957

Board Regular
Joined
Sep 30, 2014
Messages
182
Office Version
  1. 365
Platform
  1. Windows
Greetings,

I have code that converts a range of numbers from negative to positive.

VBA Code:
Sub negative_positive()
Dim conv As Range

'convert negative numbers to positive in the selection
For Each conv In Selection
If conv.Value < 0 Then
conv.Value = -conv.Value

ElseIf conv.Value > 0 Then
conv.Value = "0"

End If
Next conv

End Sub

I'm trying to apply this to a Table and can't seem to get the syntax right. I've not used VBA with tables before. I've tried many variations of the following with no luck

VBA Code:
Sub Neg2Pos()
Dim vList As ListObject
Dim vRow As ListRow
Dim vCol As ListColumn

Worksheets("Volume Data").Activate

Set vList = ActiveSheet.ListObjects("TblVolData")
Set vCol = ActiveSheet.ListObjects("TblVolData").ListColumns("Qty")

For Each vRow In vList
    If vCol.Value < 0 Then
    vCol.Value = -vCol.Value
    
    ElseIf vCol.Value > 0 Then
    vCol.Value = "0"
    
    End If
    Next vRow
    
End Sub  'Neg2Pos

Any assistance would be most appreciated.

Thank you,
~ Phil
 

Excel Facts

Who is Mr Spreadsheet?
Author John Walkenbach was Mr Spreadsheet until his retirement in June 2019.
How about
VBA Code:
Sub Neg2Pos()
Dim vCol As ListColumn
Dim vRow As Range
Worksheets("Volume Data").Activate

Set vCol = ActiveSheet.ListObjects("TblVolData").ListColumns("Qty")

For Each vRow In vCol.DataBodyRange
    If vRow.Value < 0 Then
    vRow.Value = -vRow.Value
    
    ElseIf vRow.Value > 0 Then
    vRow.Value = "0"
    
    End If
    Next vRow
    
End Sub
 
Upvote 0
Solution
Thank you again for your help Fluff. It seems you are always there when I run into trouble and I can't tell you how much I appreciate it.
 
Upvote 0
Glad to help & thanks for the feedback.
 
Upvote 0

Forum statistics

Threads
1,214,376
Messages
6,119,181
Members
448,871
Latest member
hengshankouniuniu

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