0

The goal of my macro is to prompt user to select a cell, then read this cell's value and assign it to a string variable.

This is my code:

Sub update_custdb_from_id_number_to_the_right()
   Application.ScreenUpdating = False
   'Application.DisplayAlerts = False

   Dim wordApp As Word.Application
   Dim wordDoc As Word.Document              
   Dim excelApp As Excel.Application
   Dim mySheet As Excel.Worksheet
   Dim Para As Word.Paragraph
   Dim wordRng As Word.Range
   Dim fullName As Word.Range
   Dim exclRng As Excel.Range
   Dim scndRng As Excel.Range

   Dim pStart As Long
   Dim pEnd As Long
   Dim Length As Long
   Dim startPos As Long
   Dim endPos As Long
   Dim parNmbr As Long
   Dim x As Long
   Dim flag As Boolean
   Dim sCell As String
   Dim intRow As Integer

   'Assigning object variables and values
   Set wordApp = GetObject(, "Word.Application")       
   Set excelApp = GetObject(, "Excel.Application")   
   Set wordDoc = wordApp.ActiveDocument
   Set mySheet = Application.ActiveWorkbook.ActiveSheet
   Set wordRng = wordApp.ActiveDocument.Content
   Set scndRng = ActiveSheet.Range("A1")

   sCell = ActiveWorkbook.ActiveSheet.Range(Application.InputBox(Prompt:="Pick the Cell", Type:=8)).Value              'sCell = Range(Application.ActiveWorkbook.ActiveSheet.InputBox(Prompt:="Pick the Cell", Type:=8)).Value
   ' Application-defined or object-defined error
   sCell = Trim(sCell)
   Debug.Print sCell
   intRow = 1
   x = 11
End Sub

While running this macro I get Application-defined or object-defined error in sCell = ActiveWorkbook.ActiveSheet.Range(Application.InputBox(Prompt:="Pick the Cell", Type:=8)).Value line. I press F8 to debug the line above and the InputBox appears.

I then type B4 or B6 into the InputBox and when I press OK the Application-defined or object-defined error. appears.

enter image description here

Looking 4 solution I've read this thread thread because this line previously was returning the Run-time error '1004' : Method 'Range' of object'_Global' failed error, so I've changed it a bit and now it's giving me this Application-defined or object-defined error.

What do I need to write to prompt user to select cell during macro execution and later assign this cell's value to a string variable.

Last thing is to Debug.Print sCell into the immediate window.

byte me
  • 770
  • 6
  • 13
michal roesler
  • 479
  • 2
  • 9
  • 26
  • 2
    Type 8 is already a range so you don't need to wrap it another range. – SJR May 26 '20 at 12:01
  • I've deleted what you suggested and now the line looks like this `sCell = Application.InputBox(Prompt:="Pick the Cell", Type:=8).Value` and the code works fine. – michal roesler May 26 '20 at 12:16

0 Answers0