how to find the excel file is already open

s_balasubramanian

New Member
Joined
Feb 7, 2022
Messages
8
Office Version
  1. 2019
  2. 2016
  3. 2013
Platform
  1. Windows
im new learner in vba

i have opened file (part1.xlsx) and copy the active cell content. (Vendorcode: 200050)

i want to filter the records in another file in part2.xlsx pertains to that vendor code 200050 (3rd column).

if part2.xlsx file is already opened in readonly, the msgbox1 is appear.

if i press yes copied value is not coming

if i press no the prog error is shown as msgbox2

Guide me how to proceed the next step of prog if file is already open
 

Attachments

  • msgbox2.png
    msgbox2.png
    6.2 KB · Views: 6
  • msgbox1.png
    msgbox1.png
    6.9 KB · Views: 7

Excel Facts

What is the fastest way to copy a formula?
If A2:A50000 contain data. Enter a formula in B2. Select B2. Double-click the Fill Handle and Excel will shoot the formula down to B50000.
Could you post you existing code?
Please use the available code tags. It makes your code much easier to read/debug & copy. My signature block below has more details.
 
Upvote 0
Sub p2_1020_vc()

' Ctrl + Shift + H

ActiveCell.Copy

Workbooks.Open "P:\Part 2\Part2_1020.XLSX", , True

Range("a1").pastespecial Paste:=xlPasteValues
Application.CutCopyMode = False

Range("H1").Select
ActiveCell.FormulaR1C1 = _
"=IF(OR(LEFT(RC[-7],1)=""1"",LEFT(RC[-7],1)=""2""),""vc"",IF(LEFT(RC[-7],1)=""6"",""PO"",""Vname""))"
If Range("h1") = "vc" Then
Range("b1").Value = Left(Range("a1"), 6)
Selection.AutoFilter
ActiveSheet.Range("$A$1:$AG$6000").AutoFilter Field:=2, Criteria1:=Cells(1, 2).Value
Range("B1").Value = "Vendor"
ElseIf Range("h1") = "PO" Then
Range("f1").Value = Left(Range("a1"), 10)
Selection.AutoFilter
ActiveSheet.Range("$A$1:$AG$6000").AutoFilter Field:=6, Criteria1:=Cells(1, 6).Value
Range("f1").Value = "PO No"
Else
Range("f1").Value = Range("a1").Value
Selection.AutoFilter
ActiveSheet.Range("$A$1:$AG$6000").AutoFilter Field:=5, Criteria1:=Cells(1, 5).Value
Range("E1").Value = "Vendor Name"

End If


Range("a1").Value = "Plant"

End Sub

VBA Code:
Sub p2_1020_vc()

' Ctrl + Shift + H

  ActiveCell.Copy

   Workbooks.Open "P:\Part 2\Part2_1020.XLSX", , True
   
    Range("a1").pastespecial Paste:=xlPasteValues
    Application.CutCopyMode = False
   
     Range("H1").Select
    ActiveCell.FormulaR1C1 = _
        "=IF(OR(LEFT(RC[-7],1)=""1"",LEFT(RC[-7],1)=""2""),""vc"",IF(LEFT(RC[-7],1)=""6"",""PO"",""Vname""))"
       If Range("h1") = "vc" Then
    Range("b1").Value = Left(Range("a1"), 6)
        Selection.AutoFilter
         ActiveSheet.Range("$A$1:$AG$6000").AutoFilter Field:=2, Criteria1:=Cells(1, 2).Value
            Range("B1").Value = "Vendor"
         ElseIf Range("h1") = "PO" Then
             Range("f1").Value = Left(Range("a1"), 10)
          Selection.AutoFilter
         ActiveSheet.Range("$A$1:$AG$6000").AutoFilter Field:=6, Criteria1:=Cells(1, 6).Value
          Range("f1").Value = "PO No"
         Else
         Range("f1").Value = Range("a1").Value
          Selection.AutoFilter
         ActiveSheet.Range("$A$1:$AG$6000").AutoFilter Field:=5, Criteria1:=Cells(1, 5).Value
          Range("E1").Value = "Vendor Name"
        
    End If
   
       
    Range("a1").Value = "Plant"
  
End Sub
 
Last edited by a moderator:
Upvote 0
I will look at your question later but you didn't follow my request..
Please use the available code tags. It makes your code much easier to read/debug & copy. My signature block below has more details.
You will get better and faster help if you make it as easy as possible for people to help you. Look how much easier the code at the bottom of your previous post (where I have re-posted your code with the code tags) is to read and follow compared to the top part. ;)
 
Upvote 0
I will look at your question later ..
One way to try would be to start your procedure like this

VBA Code:
Sub p2_1020_vc()
  Dim wsP2 As Workbook

  ActiveCell.Copy

  On Error Resume Next
  Set wsP2 = Workbooks("Part2_1020.XLSX")
  On Error GoTo 0
  If wsP2 Is Nothing Then Workbooks.Open "P:\Part 2\Part2_1020.XLSX", , True
  Workbooks("Part2_1020.XLSX").Activate
  
  Range("a1").PasteSpecial Paste:=xlPasteValues
 
Upvote 0
You're welcome. Thanks for the follow-up. :)
 
Upvote 0

Forum statistics

Threads
1,215,095
Messages
6,123,072
Members
449,093
Latest member
ripvw

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