Hello experts:
I am working with 2 excel files/workbooks (using Excel 2007), both files are open.
Workbook A, where I run/click the macro button. Opens a text file into excel and copy data then close excel/text file.
Now it process this data in excel sheet. It works fine.
1. One of the Sheet of workbook A, has a cell which contains the name with path of Workbook B.
(e.g. Cell: P8 has file name with path = G:\102 04-22-074-25W4 Leg 8 HZ Well Diagram.xlsx)
Now, I want vba to activate workbook B based on name from cell value. HERE I GET ERROR.
If above problem is solved then I want to paste data into one of worksheet of workbook B.
2. How to get worksheet index by providing the sheet name (read sheet name from cell value from workbook A)
(e.g. cell P10 has cell value name of worksheet = INPUT Gas, Gamma, ROP
Please help how to do it. I searched many sites, lots of stuff but can not pin point my issue.
Here is my code so far:
========================
I am working with 2 excel files/workbooks (using Excel 2007), both files are open.
Workbook A, where I run/click the macro button. Opens a text file into excel and copy data then close excel/text file.
Now it process this data in excel sheet. It works fine.
1. One of the Sheet of workbook A, has a cell which contains the name with path of Workbook B.
(e.g. Cell: P8 has file name with path = G:\102 04-22-074-25W4 Leg 8 HZ Well Diagram.xlsx)
Now, I want vba to activate workbook B based on name from cell value. HERE I GET ERROR.
If above problem is solved then I want to paste data into one of worksheet of workbook B.
2. How to get worksheet index by providing the sheet name (read sheet name from cell value from workbook A)
(e.g. cell P10 has cell value name of worksheet = INPUT Gas, Gamma, ROP
Please help how to do it. I searched many sites, lots of stuff but can not pin point my issue.
Here is my code so far:
========================
Code:
Sub GetData() 'Get data from text file (ROP, Gas, Gamma)
Dim wbDest As Workbook, wbSrc As Workbook, srcFilePath As String, rFind As Range, sFind As Range, wbTGT As Workbook, wsTgtSur As Worksheet, wsTgtGR As Worksheet
Dim wsSrc As Worksheet, SearchRange As Range, startingSheet As Integer, i As Integer, fstRow As Long, lstRow As Long, tempXLFile As String, tempTGTFile As Workbook
Set wbDest = ActiveWorkbook 'WORKBOOK A
wbDest.Sheets(1).Activate
srcFilePath = wbDest.Sheets(1).Range("P4").Value
Workbooks.OpenText Filename:=srcFilePath, Origin:=xlMSDOS, StartRow:=36 _
, DataType:=xlDelimited, TextQualifier:=xlDoubleQuote, _
ConsecutiveDelimiter:=False, Tab:=True, Semicolon:=False, Comma:=False _
, Space:=False, Other:=False, FieldInfo:=Array(1, 1), _
TrailingMinusNumbers:=True
Set wbSrc = ActiveWorkbook
tempXLFile = wbSrc.Name
Set wsSrc = wbSrc.Sheets(1)
wbDest.Sheets(3).Columns("A:F").ClearContents
wsSrc.Range("A:A").Copy Destination:=wbDest.Sheets(3).Range("A1")
Application.CutCopyMode = False
wbSrc.Close savechanges:=False
wbDest.Sheets(3).Select
wbDest.Sheets(3).Range("A:A").Select
Selection.TextToColumns Destination:=Range("A1"), DataType:=xlFixedWidth, _
FieldInfo:=Array(Array(0, 1), Array(11, 1), Array(22, 1), Array(34, 1)), _
TrailingMinusNumbers:=True
wbDest.Sheets(3).Range("C:C").Select
Selection.Cut
wbDest.Sheets(3).Range("E:E").Select
ActiveSheet.Paste
wbDest.Sheets(3).Range("C:C").Select
Selection.Delete Shift:=xlToLeft
wbDest.Sheets(3).Range("A1:D1").Select
wbDest.Sheets(3).Range(Selection, Selection.End(xlDown)).Select
Selection.Copy 'WWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWORKING UP TO HERE TESTED
Set wbTGT = Workbooks.Open(Filename:=(wbDest.Sheets(1).Range("P4").Value)) 'WORKBOOK B
Set wsTgtGR = wbTGT.Worksheets("INPUT Gas, Gamma, ROP").Range("B4") 'GIVE ERROR HERE
Set wsTgtGR = wbTGT.Sheets(11) 'GIVE ERROR HERE
wsTgtGR.Range("B4").Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
Application.CutCopyMode = False
End sub