Excel VBA Course
Excel VBA Course - From Beginner to Expert

200+ Video Lessons
50+ Hours of Video
200+ Excel Guides

Become a master of VBA and Macros in Excel and learn how to automate all of your tasks in Excel with this online course. (No VBA experience required.)

View Course

subtraction between two columns

0

Hi Guys,

I  try  subtracting   values between two  columns E,F   and  insert  column   H   for  all of  the sheets ,  after  that  will  delete  column E,F   after  subtraction . it  shows  error  mismatch in this line 

With Sheets(sh).Cells(1).CurrentRegion
Sub test1()
  Dim sh As Worksheet
  For Each sh In Worksheets
    With Sheets(sh).Cells(1).CurrentRegion
        X = Application.Match("net", .Rows(1), 0)
        If IsError(X) Then
            .Columns(.Columns.Count).Copy .Columns(.Columns.Count + 1)
            With .Columns(.Columns.Count + 1)
                .Cells(1) = "NET"
                With .Offset(1).Resize(.Rows.Count - 1)
                    .FormulaR1C1 = "=rc[-2]-rc[-1]"
                    .Value = .Value
                End With
            End With
        End If
    End With
  Next
   With Sheets(sh).Cells(1).CurrentRegion
  .Columns("E:F").Delete
  End With
  Next
End Sub
Answer
Discuss

Answers

0
Selected Answer

Hasson

You declare sh as a Worksheet prior to the loop start (in bold):

Sub test1()
  Dim sh As Worksheet
  For Each sh In Worksheets

so the line:

With Sheets(sh).Cells(1).CurrentRegion

should read:

With sh.Cells(1).CurrentRegion

and likewise further down.

Hope this helps (or Willie can) since I don't have much spare time at present

Discuss

Discussion

OMG!! sometimes simple things make difference and could be obstacle to work the project .
this fixes the problem .
thanks John .
Hasson (rep: 30) Dec 16, '22 at 9:35 am
Glad to hear that worked. Thanks for selecting my answer Hasson. 
John_Ru (rep: 6152) Dec 16, '22 at 9:45 am
Add to Discussion
0

Hello Hasson and welcome,

The reason for the "Type Mismatch" error is you are trying to reference/access a worksheet - sh - which doesn't exist (plus you are missing the quotes " "). As per your included file you need to change this to "as" (with the quotes, not - as) Also the last Next before End Sub is extra and not required - delete it. The line: .Columns("E:F").Delete deletes the entire columns E & F. I assume you only intend to delete the contents below the headers. To do so I added 2 variables: Dim LRow1 As Long, LRow2 As Long. I changed

With Sheets("sh").Cells(1).CurrentRegion
  .Columns("E:F").Delete
  End With

to

With Sheets("as")
        LRow1 = Range("E2").End(xlDown).Row
        LRow2 = Range("F2").End(xlDown).Row
        Range("E2:E" & LRow1, "F2:F" & LRow2).ClearContents
    End With

Here is the complete code;

Sub test1()
  Dim sh As Worksheet, x As Variant

  Dim LRow1 As Long, LRow2 As Long

  For Each sh In Worksheets
    With Sheets("as").Cells(1).CurrentRegion
        x = Application.Match("net", .Rows(1), 0)
        If IsError(x) Then
            .Columns(.Columns.Count).Copy .Columns(.Columns.Count - 1)
            With .Columns(.Columns.Count + 1)
                .Cells(1) = "NET"
                With .Offset(1).Resize(.Rows.Count + 1)
                    .FormulaR1C1 = "=rc[-2]-rc[-1]"
                    .Value = .Value
                End With
            End With
        End If
    End With
  Next
   With Sheets("as")
        LRow1 = Range("E2").End(xlDown).Row
        LRow2 = Range("F2").End(xlDown).Row
        Range("E2:E" & LRow1, "F2:F" & LRow2).ClearContents
    End With

End Sub

Cheers   :-)

Discuss

Discussion

Hi Willie,
The reason for the "Type Mismatch" error is you are trying to reference/access a worksheet - sh - which doesn't exist (plus you are missing the quotes " "). As per your included file you need to change this to "as"
No !!    the  code  should do  loop throught  for  all  of  the  sheets  as  I  said in  OP  and  insert  column   H   for  all of  the sheets 
you  can't  specify   specific  sheet  to  do  that  .
please  check  my  explenation  again
Hasson (rep: 30) Dec 16, '22 at 5:40 am
Hi Hasson,
My appologies for rushing to provide an answer, I didn't read your post as thoroughly as John did. (short on time; my bad) In hind-sight, my answer would have been the same as John's if I hadn't rushed things. I'm glad John chimed in and gave you the correct solution.

Cheers   :-)
WillieD24 (rep: 557) Dec 16, '22 at 11:58 am
Add to Discussion


Answer the Question

You must create an account to use the forum. Create an Account or Login