problems with lines of VBA code

sts8500man

Board Regular
Joined
Jul 12, 2012
Messages
77
Office Version
  1. 365
Platform
  1. Windows
Thanks to some previous communications on mrexcel, I obtained some help generating the subroutine below. The only problem is that it quits working under each of the following conditions:

  1. If “ABC” is not contained within Cell C7 it stops at this line of code –
.Copy wbR.Sheets(1).Range("B10")

  1. If “ABC” is contained within Cell C7 it stops at this line of code --
.Range("F7").Copy wbR.Sheets(1).Range("B10")

Does anyone know what the problem is here and how I might fix it?

Code:
Sub Get_ABC_value()
'
' Get_ABC_value Macro
'

'
Const TxtToFind = "ABC"
Dim wbS As Workbook: Set wbS = Workbooks("download - source ABC.xls")
Dim wbR As Workbook: Set wbR = Workbooks("results.xlsm")
With wbS.Sheets(1)
If InStr(1, .Range("C7").Value, TxtToFind) > 0 Then
.Range("F7").Copy wbR.Sheets(1).Range("B10")
Else
.Rows(7).Insert
.Range("C7").Value = TxtToFind
With .Range("F7")
.Value = 0
.NumberFormat = "0.0"
.Copy wbR.Sheets(1).Range("B10")
End With
End If
End With
wbS.Save
wbR.Save
wbS.Close False
wbR.Close False
End Sub
Code:
 

Excel Facts

Which came first: VisiCalc or Lotus 1-2-3?
Dan Bricklin and Bob Frankston debuted VisiCalc in 1979 as a Visible Calculator. Lotus 1-2-3 debuted in the early 1980's, from Mitch Kapor.
Thank you ranman256. I will be trying your suggested solution some more, but upon first try it did not work. That may be because I am not a programmer and therefore do not know where to insert your proposed solution into my code. I am trying to do it by trial and error and will report back to you when I exhaust my knowledge of how to do it regardless of whether it works or not. Thanks again for responding.
 
Upvote 0
What error message do you get?
 
Upvote 0
I do not get an error message. The screen just goes blank. It appears that Excel shuts down. To get back to it I have to restart it but it appears that no damage was done to any of the code. It just stops and goes blank?
 
Upvote 0
What happens if you add the parts in blue as shown
Code:
Sub Get_ABC_value()
'
' Get_ABC_value Macro
'

   [COLOR=#0000ff]With Application
      .ScreenUpdating = False
      .Calculation = xlCalculationManual
      .EnableEvents = False
   End With[/COLOR]
   Const TxtToFind = "ABC"
   Dim wbS As Workbook: Set wbS = Workbooks("download - source ABC.xls")
   Dim wbR As Workbook: Set wbR = Workbooks("results.xlsm")
   With wbS.Sheets(1)
      If InStr(1, .Range("C7").Value, TxtToFind) > 0 Then
         .Range("F7").Copy wbR.Sheets(1).Range("B10")
      Else
         .Rows(7).Insert
         .Range("C7").Value = TxtToFind
         With .Range("F7")
            .Value = 0
            .NumberFormat = "0.0"
            .Copy wbR.Sheets(1).Range("B10")
         End With
      End If
   End With
   wbS.Save
   wbR.Save
   wbS.Close False
   wbR.Close False
  [COLOR=#0000ff] With Application
      .ScreenUpdating = True
      .Calculation = xlCalculationAutomatic
      .EnableEvents = True
   End With
[/COLOR]
End Sub
 
Upvote 0
Same thing. Screen goes blank and the files I am using all close. I have to X out then reload the files but no damage to them was done. It does put in the 0.00 in Cell F7, but then all goes blank.
 
Upvote 0
In that case it sounds like one of your workbooks maybe corrupt.
 
Upvote 0
That is what I was afraid you would say. I guess I have to redo the thing from scratch. Maybe I can at least copy and paste the data into a new workbook.

Thank you for all your help and willingness to do so.

sts8500man
 
Upvote 0
Sorry we couldn't have been more help & thanks for the feedback
 
Upvote 0

Forum statistics

Threads
1,223,903
Messages
6,175,284
Members
452,630
Latest member
OdubiYouth

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