跨页的参考问题用于循环活动单元格

时间:2015-06-29 10:50:03

标签: excel vba excel-vba

我正在尝试将以下2个子组合在一个范围内循环,获取工作表4活动单元格中的值,然后包含'自动过滤表格中的值"数据库"。

这是一个参考问题,因为Autofilter系列是错误的。

以下是代码:

选项明确

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>

// length of the longest word in dictionary
#define LENGTH 45
// dictionary file
#define DICTIONARY "large"

// prototypes
char **load(const char *dictionary);
void print(char **);

int main(void)
{
    char **dictionary;

    dictionary = load(DICTIONARY);
    if (dictionary == NULL)
        return -1;
    print(dictionary);

    /* Don't forget to free resources, 
       you might need to do it while
       the program is still running,
       leaked resources might quickly 
       become a problem.
     */
    for (int i = 0 ; dictionary[i] != NULL ; ++i)
        free(dictionary[i]);
    free(dictionary);

    return 0;  
}
/**
 * Loads dictionary into memory.
 */
char **load(const char *dictionary)
{
    // open dictionary file
    FILE  *dict_file;    
    size_t dict_size;
    char **dict;
    char   word[LENGTH + 1];
    size_t word_length;
    size_t dict_index;
    dict_file = fopen(dictionary, "r");
    if (dict_file == NULL) /* you should be able to notify this */
        return NULL; /* failure to open file */
    // compute size of dictionary
    for (int c = fgetc(dict_file); c != EOF; c = fgetc(dict_file))
    {
        // look for '\n' (one '\n' means one word)
        if (c == '\n')
        {
            dict_size++;
        }
    }
    // return to beginning of file
    fseek(dict_file, 0, SEEK_SET);

    // local array
    dict = malloc((1 + dict_size) * sizeof(*dict));
    /*                    ^ add a sentinel to avoid storing the number of words */
    if (dict == NULL)
        return NULL;

    // variables for reading
    word_length = 0;
    dict_index = 0;
    // iteration over characters
    for (int c = fgetc(dict_file); c != EOF; c = fgetc(dict_file))
    {
        // allow only letters
        if (c != '\n')
        {
            // append character to word
            word[word_length] = c;
            word_length++;
        }
        // if c = \n and some letters're already in the word
        else if (word_length > 0)
        {
            // terminate current word
            word[word_length] = '\0';

            //write word to local dictionary
            dict[dict_index] = malloc(word_length + 1);
            if (dict[dict_index] != NULL)
            {
                strcpy(dict[dict_index], word);
                dict_index++;
            }
            // prepare for next word
            word_length = 0;
        }
    }
    dict[dict_index] = NULL;
    /* We put a sentinel here so that we can find the last word ... */
    return dict;
}

/**
 * Prints dictionary.
 */
void print(char **dict)
{
    for (int i = 0 ; dict[i] != NULL ; i++)
        printf("%s %p\n", dict[i], (void *) dict[i]);
}

我有这个&#39;对象不支持这个属性或方法&#39;最后一行的错误。正在过滤的单元格的值是一个顺序向下移动到A列od表格的范围&#34; 4&#34;

1 个答案:

答案 0 :(得分:0)

使用Option Explicit做得好,但使用Select方法和ActiveCell时遇到问题。相反,使用Range变量,如下所示:

Sub Test2()

Dim targetCell As Range

    Set targetCell = ThisWorkbook.Sheets("4").Range("A2")
  'Set Do loop to stop when an empty cell is reached.
  Do Until IsEmpty(targetCell)
      Call FILTER1(targetCell)
      Set targetCell = targetCell.Offset(RowOffset:=1)
  Loop
 End Sub

Sub FILTER1(ByRef searchedCell As Range)
        ThisWorkbook.Sheets("Database").Range("q2").AutoFilter Field:=17, Criteria1:="=*" & searchedCell.Value & "*", Operator:=xlAnd
End Sub

据推测,你有一些其他的代码可以在FILTER1中使用自动过滤后的数据吗?