Хотите кодирование Excel в VB6 - PullRequest
0 голосов
/ 19 марта 2011

Я хочу получить значение столбца A файла Sheet1 файла Excel в строку переменных, и после изменения его значения я хочу вставить его в столбец A другого листа 2 того же файла Excel.

1 Ответ

1 голос
/ 18 июня 2011

Вот что вам нужно сделать:

Сначала убедитесь, что на ссылки на EXCEL есть ссылки в ваших ссылках VB6, для этого зайдите в: Проект> Ссылки> Библиотека объектов Microsoft Excel XX

Где XXверсия Office, установленная на ПК.

Dim objEX As New Excel.Application

objEX.Visible = True 
' Optional if you want to see what is going on in EXCEL 
' while your code is being executed.


    objEX.Workbooks.Open "C:\My Files\Filename.xls"    
        'Make sure you put the right path of the excel workbook you want to open

With objEX
    'If you know the name of the sheet you want to read from 
    ' then use this code
    .Sheets("FIRST_Sheet_Name_Here").Activate

    'Otherwise, you may only know that the sheet is 
    ' physically the first sheet in that workbook, then use this code:
    .Sheets(1).Activate

    Dim myValue As Double

    myValue = .Range("A1").Value  
   ' Change A1 to whatever Cell you want to read from the sheet

    'Now we will do something with the value that we read and 
    ' then will save it back to EXCEL in sheet 2 and cell B2 for example
    myValue = Val(myValue) + 1000

    .Sheets("SECOND_Sheet_Name_Here").Activate   
    'if you know that name of the second sheet


    .Sheets(2).Activate                          
    'if you don't know the name, but know the location

    .Range("B2").Value = myValue                 
    ' This will write the variable to the location B2 in sheet 2



    .ActiveWorkbook.Save                         
    'Saves the changes you have done


    .ActiveWorkbook.Close                        
    'Close the workbooks but keeps Excel application open


    .Quit                                        
    'Quits excel instance and releases the process from task manager

End With

    Set objEX = Nothing            
   'Garbage Collection and making sure memory is released to other processes.
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...